// 通用通知函数
function ShowNotice(config) {
    const {
      message = '操作成功',
      type = 'info',
      duration = 2500,
      position = 'top-right'
    } = config || {};
  
    // 创建容器
    let container = document.querySelector('.messagebox-notification-container');
    if (!container) {
      container = document.createElement('div');
      container.className = 'messagebox-notification-container';
      document.body.appendChild(container);
    }
  
    // 创建通知元素
    const notice = document.createElement('div');
    notice.className = `messagebox-notification ${type}`;
    
    // 添加图标
    const icons = {
      success: '✓',
      info: 'ⓘ',
      warning: '⚠'
    };
    
    notice.innerHTML = `
      <span class="messagebox-notification-icon">${icons[type] || ''}</span>
      <span class="messagebox-notification-content">${message}</span>
    `;
  
    // 添加到容器
    container.appendChild(notice);
  
    // 自动移除
    setTimeout(() => {
      notice.style.opacity = '0';
      setTimeout(() => notice.remove(), 300);
    }, duration);
  }
  
  // 通用弹窗组件
  async function ShowErrorModal(message, options = {}) {
    // 清理历史弹窗
    clearErrorMessages();
  
    // 合并默认配置
    const config = {
      title: '操作提示',
      buttons: [
        { 
          text: '取消', 
          type: 'cancel',
          onClick: () => {} 
        },
        { 
          text: '重试', 
          type: 'retry',
          onClick: () => handleLogin(new Event('submit'))
        }
      ],
      ...options
    };
  
    // 创建弹窗容器
    const modal = document.createElement('div');
    modal.className = 'messagebox-modal';
    
    // 生成按钮HTML
    const buttonsHTML = config.buttons.map(btn => `
      <button
        class="messagebox-btn ${btn.type === 'cancel' ? 'messagebox-btn-secondary' : 'messagebox-btn-primary'}"
        data-type="${btn.type}"
      >
        ${btn.text}
      </button>
    `).join('');
  
    // 插入DOM结构
    modal.innerHTML = `
      <div class="messagebox-content">
        <div class="messagebox-close">&times;</div>
        <h3 class="messagebox-title">${config.title}</h3>
        <p class="messagebox-message">${message}</p>
        <div class="messagebox-actions">${buttonsHTML}</div>
      </div>
    `;
  
    // 返回Promise以便await
    return new Promise((resolve) => {
      // 绑定关闭事件
      const closeModal = () => {
        modal.remove();
        resolve({ action: 'close' });
      };
  
      // 关闭按钮事件
      modal.querySelector('.messagebox-close').onclick = closeModal;
  
      // 绑定按钮事件
      config.buttons.forEach(btnConfig => {
        const btn = modal.querySelector(`[data-type="${btnConfig.type}"]`);
        btn.onclick = () => {
          btnConfig.onClick();
          modal.remove();
          resolve({ action: btnConfig.type });
        };
      });
  
      document.body.appendChild(modal);
    });
  }
  
  // 辅助函数
  function clearErrorMessages() {
    const existingModals = document.querySelectorAll('.messagebox-modal');
    existingModals.forEach(modal => modal.remove());
  }